home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / mui / mui-tools / multiuser / src / support / setowner37.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  5KB  |  174 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Set the owner of a file - AmigaOS 2.04 (V37+) version        *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <dos/dos.h>
  14. #include <dos/dosasl.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <string.h>
  18. #include <libraries/multiuser.h>
  19. #include <proto/multiuser.h>
  20.  
  21. #include "SetOwner37_rev.h"
  22.  
  23. #include "Locale.h"
  24.  
  25. char __VersTag__[] = VERSTAG;
  26.  
  27.  
  28. static BOOL MySetOwner(char *name, LONG owner, struct DosLibrary *DOSBase);
  29.  
  30.  
  31. int __saveds Start(char *arg)
  32. {
  33.     struct ExecBase *SysBase;
  34.     struct DosLibrary *DOSBase;
  35.     struct muBase *muBase = NULL;
  36.     struct RDArgs *args;
  37.     LONG argarray[] = {
  38. #define argFILE    0
  39. #define argUSER    1
  40. #define argGROUP    2
  41. #define argNOBODY    3
  42. #define argALL        4
  43. #define argQUIET    5
  44.         NULL, NULL, NULL, NULL, NULL, NULL
  45.     };
  46.     ULONG user = NULL;
  47.     struct muUserInfo *uinfo;
  48.     struct muGroupInfo *ginfo;
  49.     struct AnchorPath *anchor;
  50.     int rc = RETURN_OK;
  51.     LONG error = NULL;
  52.     BPTR dir;
  53.     struct LocaleInfo li;
  54.  
  55.     SysBase = *(struct ExecBase **)4;
  56.     
  57.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  58.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  59.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  60.         goto Exit;
  61.     }
  62.  
  63.     OpenLoc(&li);
  64.  
  65.     args = ReadArgs("FILE/A,USER,GROUP,NOBODY/S,ALL/S,QUIET/S", argarray, NULL);
  66.     if (!args)
  67.         error = IoErr();
  68.     else if (argarray[argUSER] && argarray[argNOBODY]) {
  69.         PutStr(GetLocS(&li,MSG_INVALID_OPTIONS));
  70.         rc = RETURN_ERROR;
  71.     } else {
  72.         if (argarray[argUSER]) {
  73.             if (uinfo = muAllocUserInfo()) {
  74.                 strncpy(uinfo->UserID, (char *)argarray[argUSER], muUSERIDSIZE-1);
  75.                 uinfo->UserID[muUSERIDSIZE-1] = '\0';
  76.                 if (muGetUserInfo(uinfo, muKeyType_UserID))
  77.                     user = (uinfo->uid<<16)|uinfo->gid;
  78.                 muFreeUserInfo(uinfo);
  79.             } else {
  80.                 PrintFault(IoErr(), NULL);
  81.                 goto Exit;
  82.             }
  83.             if (!user) {
  84.                 VPrintf(GetLocS(&li,MSG_UNKNOWN_USER), &argarray[argUSER]);
  85.                 rc = RETURN_ERROR;
  86.             }
  87.         } else if (!argarray[argNOBODY])
  88.             user = muGetTaskOwner(NULL);
  89.         if (argarray[argGROUP]) {
  90.             if (ginfo = muAllocGroupInfo()) {
  91.                 strncpy(ginfo->GroupID, (char *)argarray[argGROUP],
  92.                           muGROUPIDSIZE-1);
  93.                 ginfo->GroupID[muGROUPIDSIZE-1] = '\0';
  94.                 if (muGetGroupInfo(ginfo, muKeyType_GroupID))
  95.                     user = user & muMASK_UID | ginfo->gid;
  96.                 else {
  97.                     VPrintf(GetLocS(&li,MSG_UNKNOWN_GROUP), &argarray[argGROUP]);
  98.                     rc = RETURN_ERROR;
  99.                 }
  100.                 muFreeGroupInfo(ginfo);                
  101.             } else {
  102.                 PrintFault(IoErr(), NULL);
  103.                 goto Exit;
  104.             }
  105.         }
  106.         if (!rc)
  107.             if (anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath)+1024,
  108.                                                                      MEMF_CLEAR)) {
  109.                 anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  110.                 anchor->ap_Flags = APF_DOWILD;
  111.                 anchor->ap_Strlen = 1024;
  112.                 if (!(error = MatchFirst((char *)argarray[argFILE], anchor))) {
  113.                     do
  114.                         if (anchor->ap_Flags & APF_DIDDIR)
  115.                             anchor->ap_Flags &= ~APF_DIDDIR;
  116.                         else {
  117.                             if (argarray[argALL] && (anchor->ap_Info.fib_DirEntryType > 0))
  118.                                 anchor->ap_Flags |= APF_DODIR;
  119.                             dir = CurrentDir(DupLock(anchor->ap_Last->an_Lock));
  120.                             if (!MySetOwner(anchor->ap_Info.fib_FileName, user, DOSBase)) {
  121.                                 PutStr(anchor->ap_Buf);
  122.                                 PrintFault(IoErr(), " ");
  123.                             } else if (!argarray[argQUIET]) {
  124.                                 PutStr(anchor->ap_Buf);
  125.                                 if (anchor->ap_Info.fib_DirEntryType > 0)
  126.                                     PutStr("(Dir)");
  127.                                 PutStr(GetLocS(&li,MSG_DONE));
  128.                             }
  129.                             UnLock(CurrentDir(dir));
  130.                         }
  131.                     while (!(error = MatchNext(anchor)));
  132.                     if (error == ERROR_NO_MORE_ENTRIES)
  133.                         error = NULL;
  134.                 } else if (error == ERROR_NO_MORE_ENTRIES)
  135.                     error = ERROR_OBJECT_NOT_FOUND;
  136.                 MatchEnd(anchor);
  137.                 FreeVec(anchor);
  138.             } else
  139.                 error = IoErr();
  140.     }
  141.     FreeArgs(args);
  142.     if (error) {
  143.         PrintFault(error, NULL);
  144.         rc = RETURN_ERROR;
  145.     }
  146.  
  147.     CloseLoc(&li);
  148.  
  149. Exit:
  150.     CloseLibrary((struct Library *)muBase);
  151.     CloseLibrary((struct Library *)DOSBase);
  152.  
  153.     return(rc);
  154. }
  155.  
  156.  
  157. static BOOL MySetOwner(char *name, LONG owner, struct DosLibrary *DOSBase)
  158. {
  159.     BOOL rc = FALSE;
  160.  
  161.     struct DevProc *dp;
  162.     BPTR fl;
  163.  
  164.     if (dp = GetDeviceProc(name, NULL)) {
  165.         if (fl = Lock(name, ACCESS_READ)) {
  166.             rc = DoPkt(dp->dvp_Port, ACTION_SET_OWNER, NULL, fl, MKBADDR("\0"),
  167.                           owner, NULL);
  168.             UnLock(fl);
  169.         }
  170.         FreeDeviceProc(dp);
  171.     }
  172.     return(rc);
  173. }
  174.